﻿using System.Windows.Controls;
using System.Windows.Input;
using GoogleApi;
using Microsoft.Phone.Controls;

namespace TestAutoComplete
{
    public partial class MainPage
    {
        //create a private autocomplete object
        private AutoCompleteRequest _auto;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Loaded += MainPageLoaded;
        }

        void MainPageLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            _auto = new AutoCompleteRequest("your key here")
            {

                Latitude = 32.2217,
                Longitude = -110.9258,
                CountryComponent = "US",
                Language = AutoCompleteRequest.LanguageName.English,
                Place = AutoCompleteRequest.PlaceType.Establishment,
                Radius = 50000

            };

            _auto.AutoCompleteRequestCompleted += (s, f) => Dispatcher.BeginInvoke(delegate
            {
                _autoCompleteBox.ItemsSource = f.Results;
                _autoCompleteBox.PopulateComplete();
            });
        }

        private void AutoCompleteBoxKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.Focus();
            }
        }

        private void AutoCompleteBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var txtBox = sender as AutoCompleteBox;
            if (txtBox != null && txtBox.Text.Length > 0)
            {
                this.Focus();

            }
        }

   

        private void LocSearchPopulating(object sender, PopulatingEventArgs e)
        {
            e.Cancel = true;
            _auto.StartAutoCompleteAsync(_autoCompleteBox.Text);
        }
    }
}